package com.ljt.aicodehelp.ai.tool;


import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

@Slf4j
public class InterviewQuestionTool {

    // value原文为：可以搜索出有关编程的相关面试题。一旦有人问到有关编程的面试题，如果有数量要求，不应该少于5条。
    @Tool(
            name = "InterviewQuestionTool",
            value = """
            Relevant programming interview questions can be searched.
            Once someone asks about programming interview questions,
             if there is a quantity requirement, it should not be less than 5.
            """
    )
    public String searchInterviewQuestions(@P(value = "the keyword to search") String keyword) {
        var questionList = new ArrayList<String>();
        var encodedKeyword = URLEncoder.encode(keyword, StandardCharsets.UTF_8);
        var url = "http://www.mianshiya.com/search/all?searchText=" + encodedKeyword;


        Document document = null;
        try {
            document = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0")
                    .timeout(5000)
                    .get();
        } catch (IOException e) {
            log.error("面试问题工具错误！！",e);
            return e.getMessage();
        }

        var elements = document.select(".ant-table-cell > a");
        elements.forEach(elem -> questionList.add(elem.text().trim()));
        return String.join("\n",questionList);
    }
}
